@echo off
setlocal enabledelayedexpansion

set buildType=%1
set branch=%2
set clean=%3

set logFile=IOSbuildLog.log
set basePath=C:\mateurp_pipline\mateurp_ios
set projectPath=%basePath%\mateurp
if not "%buildType%"=="DEBUG" (
    if not "%buildType%"=="RELEASE" (
        echo param must be 'DEBUG' or 'RELEASE'
        exit /b 1
    )
)

if "%branch%" == "" (
	echo branch must be set
    exit /b 1
)

cd /d %basePath%

:: Update Code
echo Update Code Start
git checkout %branch%
git fetch origin || exit /b 1
git reset --hard "origin/%branch%" || exit /b 1
echo Update Code Finish

echo Check Changed
set curr_buildId=%basePath%\mateurp\Build\build_id_%buildType%.txt
set last_buildId=%basePath%\mateurp\Build\build_id_%buildType%_last.txt
set export_zip=%basePath%\mateurp\Build\iOS\%buildType%\output_xcode.zip
git log -1 --format=%%h origin/%branch%>%curr_buildId%
:: 检查文件是否存在
if exist "%last_buildId%" (
	if exist %export_zip% (
	    echo "last file exist!"
		fc /b "%curr_buildId%" "%last_buildId%" > nul
		:: 获取 FC 命令的错误级别 (Error Level)
		if !errorlevel! equ 0 (
			 echo zip exists and not need update!
			 goto eof
		)
		del %export_zip%
	)
)
echo End Check Changed

:: GenerateAll
if "%clean%" == "true" (
  echo GenerateAll Start
  "C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe"  -buildTarget iOS -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod HybridCLR.Editor.BuildAssetsCommand.Prebuild -logFile C:\Users\Wekoi\Downloads\%logFile% || exit /b 1
  echo GenerateAll Finish
)

:: Copy ClothShaderVariants
set artvariants=C:\mateurp_pipline\mate-unity-cloth\Assets\_Art\ClothShaderVariants.shadervariants
copy /Y %artvariants% %projectPath%\Assets\_Art\ClothShaderVariants.shadervariants
copy /Y %artvariants% C:\mateurp_pipline\Mate\ShaderVariants\ClothShaderVariants.shadervariants

if "%clean%" == "true" (
  rd /s /q %projectPath%\Assets\StreamingAssets
  rd /s /q %projectPath%\HybridCLRBuildCache
)
:: BuildAssets
echo BuildAssets Start
"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod HybridCLR.Editor.BuildAssetsCommand.BuildAndCopyABAOTHotUpdateDlls -logFile C:\Users\Wekoi\Downloads\%logFile% || exit /b 1
echo BuildAssets Finish

:: BuildArts
::echo BuildArts Start
::"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod HybridCLR.Editor.BuildAssetsCommand.BuildBuiltInArtAssetsAB -logFile C:\Users\Wekoi\Downloads\%logFile% || exit /b 1
::echo BuildArts Finish

:: BuildXCODE
echo BuildXCODE Start
rd /s /q %projectPath%\Build\IOS\%buildType%
set "buildType=%buildType%"
"C:\Program Files\Unity\Hub\Editor\2022.3.61f1c1\Editor\Unity.exe" -batchmode -nographics -quit -projectPath "%projectPath%" -executeMethod BuildTool.BuildIPAScript  -logFile C:\Users\Wekoi\Downloads\%logFile% || exit /b 1
echo BuildXCODE Finish

echo ZipFramework Start
"C:\Program Files\7-Zip\7z.exe" a -r %export_zip% "C:\mateurp_pipline\mateurp_ios\mateurp\Build\IOS\%buildType%\output_xcode\*"
echo ZipFramework End
copy /Y %curr_buildId% %last_buildId%
:eof
endlocal